package org.dahuatech.ssh.dao.organization;

import java.util.List;

import org.dahuatech.ssh.common.orm.hibernate.HibernateDao;
import org.dahuatech.ssh.entity.organization.Organization;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

@Component
public class OrganizationDao extends HibernateDao<Organization, Long> {
	private static String TABLE_NAME="sys_organization";
	
	/**
	 * 获取最大的部门编号
	 * @param code
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getOrganizationSubCode(String code){
		String queryString = "select max(code) from "+ TABLE_NAME +" t where t.code like '"+ code+"___'";
		List<String> list= this.getSession().createSQLQuery(queryString).list();
		if(list.size()==0){
			return "001";
		}else{
			return list.get(0);
		}
	}
	
	/**
	 * 获取子部门
	 * @param code
	 * @return
	 */
	public List<Organization> getSubOrganizationsByCode(String code){
		Criterion criterion = Restrictions.like("code", code+"___");
		return find(criterion);
	}
}
